package com.example.userpointmgr.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.userpointmgr.dao.*;
import com.example.userpointmgr.map.UserMapper;
import com.example.userpointmgr.tool.PageParam;
import com.example.userpointmgr.tool.PageResult;
import com.example.userpointmgr.tool.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<UserPo> findAllUsers(PageParam pageParam) {
        //获取总数
        int total=userMapper.total();
        List<UserPo> userPoList = userMapper.findAllUsers(pageParam.getOffset(), pageParam.getPageSize());
        return new  PageResult<>(total,userPoList);
    }

    @Override
    public ResponseResult getUserInfo(String userphonenumber) {
        LambdaQueryWrapper<UserPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPo::getPhoneNumber, userphonenumber).eq(UserPo::getIsDelete, 0);

        UserPo userPo = userMapper.selectOne(queryWrapper);
        if (userPo == null) {
            return ResponseResult.fail("用户不存在");
        }
        UserVo userVo = new UserVo();
        userVo.setUserGroup(userPo.getUserGroup());
        userVo.setRealName(userPo.getRealName());
        userVo.setNickName(userPo.getNickName());
        userVo.setAvatar(userPo.getAvatar());
        userVo.setUserNum(userPo.getUserNum());
        userVo.setPhoneNumber(userPo.getPhoneNumber());
        return ResponseResult.success(userVo);
    }

    @Override
    public ResponseResult getUserInfoByOpenid(String openid) {
        LambdaQueryWrapper<UserPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPo::getOpenid, openid).eq(UserPo::getIsDelete, 0);

        UserPo userPo = userMapper.selectOne(queryWrapper);
        if (userPo == null) {
            return ResponseResult.fail("您还没绑定，请注册");
        }
        UserVo userVo = new UserVo();
        userVo.setUserGroup(userPo.getUserGroup());
        userVo.setRealName(userPo.getRealName());
        userVo.setNickName(userPo.getNickName());
        userVo.setAvatar(userPo.getAvatar());
        userVo.setUserNum(userPo.getUserNum());
        userVo.setPhoneNumber(userPo.getPhoneNumber());
        userVo.setUserPoints(userPo.getUserPoints());
        return ResponseResult.success(userVo);
    }

    @Override
    public ResponseResult userRegister(UserRegisterDTO userRegisterDTO) {
        //查询手机号是否重复
        LambdaQueryWrapper<UserPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPo::getPhoneNumber, userRegisterDTO.getPhoneNumber()).eq(UserPo::getIsDelete, 0);
        UserPo userPo = userMapper.selectOne(queryWrapper);
        //查询工号是否重复
        UserPo userPo1 = userMapper.selectOneByUserNum(userRegisterDTO.getUserNum());

        UserPo userPo2 = userMapper.selectOneByOpenid(userRegisterDTO.getOpenid());
        if (userPo == null) {
            if (userPo2 == null) {
                if (userPo1 == null) {
                    userPo = new UserPo();
                    userPo.setPhoneNumber(userRegisterDTO.getPhoneNumber());
                    userPo.setRealName(userRegisterDTO.getRealName());
                    userPo.setNickName(userRegisterDTO.getNickName());
                    userPo.setAvatar(userRegisterDTO.getAvatar());
                    userPo.setUserNum(userRegisterDTO.getUserNum());
                    userPo.setUserGroup(userRegisterDTO.getUserGroup());
                    userPo.setOpenid(userRegisterDTO.getOpenid());
                    userPo.setUserPoints(0);
                    userPo.setAddTime(new Date());
                    userPo.setIsDelete(0);
                    //新建默认是普通用户
                    userPo.setRole("1");
                    //自带的
//            userMapper.insert(userPo);
//mapper写的
                    userMapper.userRegister(userPo);
                    return ResponseResult.success(userPo);
                } else {
                    return ResponseResult.fail("工号重复");
                }
            } else {
                return ResponseResult.fail("微信已绑定");
            }
        } else {
            return ResponseResult.fail("该手机号已绑定");
        }

    }

    @Override
    public Integer deleteUser(String userphonenumber) {
        userMapper.deleteUserByPhoneNumber(userphonenumber);
        return 0;
    }

    @Override
    public ResponseResult updateUser(UserPointChangeDto userPointChangeDto) {
        UserPo userPo = new UserPo();
        //增加减少的积分量
        int points = userPointChangeDto.getUserPoints();
        System.out.println("opneid是：" + userPointChangeDto.getUserPoints());
        System.out.println("操作的积分" + points);
        //原有的积分
        int userPoints;
        UserPo userPo1 = userMapper.selectOneByOpenid(userPointChangeDto.getOpenid());
        if (userPo1 != null) {
            userPoints = userPo1.getUserPoints();
            System.out.println("现有积分" + userPoints);
            userPoints = userPoints + points;
            System.out.println("新积分" + userPoints);
            userPo.setUserPoints(userPoints);
            userPo.setRole(userPointChangeDto.getRole());
            userPo.setOpenid(userPointChangeDto.getOpenid());
            userMapper.updateUserByopenid(userPo);
            //查询新的积分
            UserPo userPo2 = userMapper.selectOneByOpenid(userPointChangeDto.getOpenid());
            return ResponseResult.success(userPo2);
        } else {
            return ResponseResult.fail("用户不存在请联系管理员看看该用户是否存在");
        }
    }
}
